package com.example.eshop_ssm.controller;

import com.example.eshop_ssm.annotation.CheckLogin;
import com.example.eshop_ssm.entity.TbTypeInfo;
import com.example.eshop_ssm.service.BookManagerService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/BookManagerController")
public class BookManagerController {
    @Autowired
    private BookManagerService bookManagerServicee;

    /**
     * 查询
     * @param book_name
     * @param book_author
     * @param status
     * @param type_id
     * @param page
     * @return
     */
    @CheckLogin
    @RequestMapping(value = "/select")
    public ModelAndView selectBookInfo(@RequestParam(required = false) String book_name,
                                       @RequestParam(required = false) String book_author,
                                       @RequestParam(required = false) String status,
                                       @RequestParam(required = false) String type_id,
                                       @RequestParam(required = false )String page ){
        Map<String, Object> bookInfos = bookManagerServicee.selectBookInfo(book_name,book_author,status,type_id,page);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("bookInfos",bookInfos);


        //查询类型，填充下拉框
        List<TbTypeInfo> tbTypes = bookManagerServicee.selectTypeId();
        modelAndView.addObject("tbTypes",tbTypes);
        modelAndView.setViewName("bookmanager"); //转发页面 默认转发

        return modelAndView;
    }

    /**
     * 单击删除数据
     * @param book_id
     */
    @CheckLogin
    @RequestMapping("/deleteBook/{book_id}")
    @ResponseBody
    public  HashMap<String, Object> deleteBook( @PathVariable("book_id") Integer book_id){
        //ModelAndView modelAndView = new ModelAndView();
        Integer integer = bookManagerServicee.deleteBook(book_id);
        HashMap<String, Object> effectRowMap = new HashMap<>();
        effectRowMap.put("effectRow",integer);
        // modelAndView.addObject("effectRowMap",effectRowMap);
        // modelAndView.setViewName("bookmanager");
        return effectRowMap;
    }

    /**
     * 批量修改数据 一级单击修改状态
     * @param control
     * @param bookIds
     * @return
     */
    @CheckLogin
    @RequestMapping("/updateBook")
    @ResponseBody
    public HashMap<String, Object> updateBook(String control /*字段名和控制变量*/,  String bookIds ){
        Integer integer = bookManagerServicee.updateBook(control, bookIds);
        HashMap<String, Object> effectRowMap = new HashMap<>();
        effectRowMap.put("effectRow",integer);
        return effectRowMap;
    }
}
